JavaScript Best Practices by John Au-Yeung

JavaScript Best Practices by John Au-Yeung

Author:John Au-Yeung [Au-Yeung, John]
Language: eng
Format: azw3
Published: 2020-07-15T16:00:00+00:00


Optimize Loops

We should cache values that are used in every literation in a single variable.

This is because every time we do this, the CPU has to access the item in memory again and again to compute its results.

Therefore, we should do this as little as possible.

For example, if we have a loop, we shouldn’t write the following:

for (let i = 0; i < arr.length; i++) { }

Instead, we should write:

let length = arr.length; for (let i = 0; i < length; i++) { }

This way, arr.length is only referenced once in our loop instead of accessing it in every iteration.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.